package cn.elegent.tx.context;


import cn.elegent.tx.core.entity.TransactionEntity;

import java.util.HashMap;
import java.util.Map;

public class ElegentACTXContext {

    //======================全局事务参数============================
    private final static ThreadLocal<TransactionEntity> myTransactionThreadLocal = new ThreadLocal<TransactionEntity>();

    public static void set(TransactionEntity data){
        myTransactionThreadLocal.set(data);
    }

    public static TransactionEntity get(){
        return myTransactionThreadLocal.get();
    }

    public static void remove(){
        myTransactionThreadLocal.remove();
        args.remove();
    }
    //========================回滚参数==========================
    private final static ThreadLocal<Map<String,Object>> args = new ThreadLocal<Map<String,Object>>();

    /**
     * 添加参数
     * key是未来获取参数时需要用到的key
     * @param key
     * @param data
     */
    public static void setSnapshot(String key,Object data){
        Map<String, Object> argsMap = args.get();
        if(argsMap==null){
            argsMap = new HashMap<>();
        }
        argsMap.put(key, data);
        args.set(argsMap);
    }

    /**
     * 获取回滚参数集
     * @return
     */
    public static Snapshot getSnapshot(){
        return new Snapshot(args.get());
    }

}
